package com.ruoyi.system.controller.Evaluating;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.EvaluationResultException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Evaluating.IndicatorEvaluationScore;
import com.ruoyi.system.domain.dto.EvaluationRecordSearchDto;
import com.ruoyi.system.domain.dto.EvaluationResultSearchDto;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Evaluating.EvaluationResult;
import com.ruoyi.system.service.Evaluating.IEvaluationResultService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评价结果Controller
 * 前端方面：api路径未改，已有前端展现页面，需要重新配置
 * 增删改查：随评价过程增，可删，不可改，可查（以及条件查询）
 * @author zdm
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/system/result")
public class EvaluationResultController extends BaseController
{
    @Autowired
    private IEvaluationResultService evaluationResultService;

    /**
     * 查询评价结果
     */
    @PreAuthorize("@ss.hasPermi('system:result:query')")
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(value="pageNum",required = false) String pageNum,
            @RequestParam(value="pageSize",required = false)String pageSize,
            EvaluationResult evaluationResult)
    {
        startPage();
        List<EvaluationResult> list = evaluationResultService.selectEvaluationResultList(evaluationResult);
        return getDataTable(list);
    }

    /**
     * 查询已公开的评价结果
     */
    @PreAuthorize("@ss.hasPermi('system:result:public')")
    @GetMapping("/list/public")
    public TableDataInfo public_list(
            @RequestParam(value="pageNum",required = false) String pageNum,
            @RequestParam(value="pageSize",required = false)String pageSize,
            EvaluationResult evaluationResult)
    {
        startPage();
        evaluationResult.setpublicStatus(1);
        List<EvaluationResult> list = evaluationResultService.selectEvaluationResultList(evaluationResult);
        return getDataTable(list);
    }
    /*
    条件查询
     */
    @PreAuthorize("@ss.hasPermi('system:result:search')")
    @GetMapping("/search")
    public TableDataInfo selectBySearch(@RequestParam(value="id",required = false) String id,
                                     @RequestParam(value="standarName",required = false) String standarName,
                                     @RequestParam(value="projectName",required = false) String projectName,
                                     @RequestParam(value="publicStatus",required = false)String status,
                                        @RequestParam(value="evaluationName",required = false)String evaluationName,
                                     @RequestParam(value="startDate",required = false) String startDate,
                                     @RequestParam(value="endDate",required = false)String endDate,
                                        @RequestParam(value="pageNum",required = false) String pageNum,
                                        @RequestParam(value="pageSize",required = false)String pageSize,
                                        @RequestParam(value="standardCode",required = false) String standardCode){

        startPage();
        EvaluationResultSearchDto evaluationResultSearchDto=new EvaluationResultSearchDto();
        evaluationResultSearchDto.setId(id);
        evaluationResultSearchDto.setStandardName(standarName);
        evaluationResultSearchDto.setProjectName(projectName);
        evaluationResultSearchDto.setStatus(status);
        evaluationResultSearchDto.setStartDate(startDate);
        evaluationResultSearchDto.setEndDate(endDate);
        evaluationResultSearchDto.setEvaluationName(evaluationName);
        evaluationResultSearchDto.setStandardCode(standardCode);
        List<EvaluationResult> list=evaluationResultService.searchEvaluationResultList(evaluationResultSearchDto);
        return  getDataTable(list);
    }
    /*
    可视化扇形图
     */
    @PreAuthorize("@ss.hasPermi('system:result:IndicatorEvaluationScore')")
    @GetMapping("/IndicatorEvaluationScore")
    public AjaxResult selectIndicatorEvaluationScoreForPercentage(@RequestParam("evaluationResultId")String evaluationResultId)
    {
        return success(evaluationResultService.selectIndicatorEvaluationScoreByResultId(evaluationResultId));
    }


    /**
     * 查询评价结果具体信息
     */
    @PreAuthorize("@ss.hasPermi('system:result:detailed')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(evaluationResultService.selectEvaluationResultById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:result:trend')")
    @GetMapping(value = "trend")
    public AjaxResult getInfo(@RequestParam(value = "standardCode",required = false)String standardCode)
    {
        if(StringUtils.isBlank(standardCode)){
            throw new EvaluationResultException("标准id不存在，请联系管理员");
        }
            return success(evaluationResultService.selectEvaluationResultTrend(standardCode));
    }
//    @PreAuthorize("@ss.hasPermi('system:result:totals')")
    @GetMapping("/totals")
    public AjaxResult getTotal() {
        return success(evaluationResultService.getTotalResultsCount());
    }


    @PreAuthorize("@ss.hasPermi('system:result:alterpublic')")
    @PutMapping("/alterpublic")
    public AjaxResult alterIsPublic(@RequestBody EvaluationResult evaluationResult)
    {
        Integer isPublic=evaluationResult.getpublicStatus();
        Long resultId=evaluationResult.getId();
        System.out.println("前端数据:"+isPublic);
        return toAjax(evaluationResultService.alterEvaluationResultIsPublic(resultId,isPublic));
    }















    /**
     * 导出【请填写功能名称】列表
     */
    //    @PreAuthorize("@ss.hasPermi('system:result:export')")
    //    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    //    @PostMapping("/export")
    //    public void export(HttpServletResponse response, EvaluationResult evaluationResult)
    //    {
    //        List<EvaluationResult> list = evaluationResultService.selectEvaluationResultList(evaluationResult);
    //        ExcelUtil<EvaluationResult> util = new ExcelUtil<EvaluationResult>(EvaluationResult.class);
    //        util.exportExcel(response, list, "【请填写功能名称】数据");
    //    }

    /**
     * 获取【请填写功能名称】详细信息
     */

    /**
     * 新增【请填写功能名称】
     */
    //@PreAuthorize("@ss.hasPermi('system:result:add')")
    //@Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    //@PostMapping
    //public AjaxResult add(@RequestBody EvaluationResult evaluationResult)
    //{
    //    return toAjax(evaluationResultService.insertEvaluationResult(evaluationResult));
    //}

    /**
     * 修改名称
     */
    @PreAuthorize("@ss.hasPermi('system:result:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EvaluationResult evaluationResult)
    {
        return toAjax(evaluationResultService.updateEvaluationResult(evaluationResult));
    }

    @PreAuthorize("@ss.hasPermi('system:result:edit')")
    @Log(title = "修改评价名称", businessType = BusinessType.UPDATE)
    @PutMapping("/name")
    public AjaxResult updateName(@RequestBody EvaluationResult evaluationResult)
    {
        String EvaluationName=evaluationResult.getEvaluationName();
        Long id=evaluationResult.getId();
        return toAjax(evaluationResultService.updateEvaluationResultbyName(EvaluationName,id));
    }
    /**
     * 删除【请填写功能名称】
     */
    //PreAuthorize("@ss.hasPermi('system:result:remove')")
    //Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	//DeleteMapping("/{ids}")
    //ublic AjaxResult remove(@PathVariable Long ids)
    //
    //   return toAjax(evaluationResultService.deleteEvaluationResultByIds(ids));
    //
}
